package com.yanxinim.cn.ui.me.redpacket;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanxinim.cn.AppConfig;
import com.yanxinim.cn.R;
import com.yanxinim.cn.bean.event.EventNotifyByTag;
import com.yanxinim.cn.bean.redpacket.Balance;
import com.yanxinim.cn.sp.UserSp;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.ui.me.redpacket.scan.ScanRechargeActivity;
import com.yanxinim.cn.ui.me.redpacket.scan.ScanWithdrawActivity;
import com.yanxinim.cn.ui.tool.ButtonColorChange;
import com.yanxinim.cn.util.EventBusHelper;
import com.yanxinim.cn.util.SkinUtils;
import com.yanxinim.cn.util.ToastUtil;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import com.yanxinim.cn.view.TipDialog;
import com.yanxinim.cn.store.AuthUserInfoActivity;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class WxPayBlance extends BaseActivity {

    public static final String RSA_PRIVATE = "";
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    private TextView mBalanceTv;
    private TextView mRechargeTv;
    private TextView mWithdrawTv;
    private TextView mScanRechargeTv;
    private TextView mScanWithdrawTv;
    private TextView mBankCardTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wx_pay_blance);
        initActionBar();
        initView();
        EventBusHelper.register(this);
        getAuthUser();
    }

    @Override
    protected void onResume() {
        // todo 提现之后回到该界面，服务端待微信响应之后才会更新余额，此时调用刷新余额的方法获取到的可能还是之前的余额，另加一个EventBus来刷新吧
        super.onResume();
        initData();
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView mTvTitle = (TextView) findViewById(R.id.tv_title_center);
        mTvTitle.setText(getString(R.string.my_purse));
        ImageView mImageView = findViewById(R.id.iv_title_right);
        mImageView.setImageDrawable(getResources().getDrawable(R.mipmap.navigation));
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 访问接口 获取记录
                Intent intent = new Intent(WxPayBlance.this, PaymentCenterActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        mBalanceTv = (TextView) findViewById(R.id.myblance);
        mRechargeTv = (TextView) findViewById(R.id.chongzhi);
        mWithdrawTv = (TextView) findViewById(R.id.quxian);
        mScanRechargeTv = (TextView) findViewById(R.id.scan_recharge);
        mScanWithdrawTv = (TextView) findViewById(R.id.scan_withdraw);
        mBankCardTv = (TextView) findViewById(R.id.tvBankCard);
        ButtonColorChange.rechargeChange(this, mRechargeTv, R.drawable.chongzhi_icon);
        ButtonColorChange.rechargeChange(this, mWithdrawTv, R.drawable.recharge_icon);
        ButtonColorChange.rechargeChange(this, mScanRechargeTv, R.drawable.chongzhi_icon);
        ButtonColorChange.rechargeChange(this, mScanWithdrawTv, R.drawable.recharge_icon);
        mWithdrawTv.setTextColor(SkinUtils.getSkin(this).getAccentColor());
        mScanWithdrawTv.setTextColor(SkinUtils.getSkin(this).getAccentColor());

        // 判断微信和支付宝是否都是关着的
        boolean isClose = (!coreManager.getConfig().enableWxPay && !coreManager.getConfig().enableAliPay);
        //如果判断为true，则说明都是关着的，提现和充值按钮都隐藏
        mRechargeTv.setVisibility(isClose ? View.GONE : View.VISIBLE);
        mWithdrawTv.setVisibility(isClose ? View.GONE : View.VISIBLE);
        if (coreManager.getConfig().isOpenManualPay) {
            mScanRechargeTv.setVisibility(View.VISIBLE);
            // 审核提现移至到提现内，如提现按钮被隐藏，审核提现则在外面显示
            mScanWithdrawTv.setVisibility(isClose ? View.VISIBLE : View.GONE);
        }

        /**
         * 充值、提现
         */
        mRechargeTv.setOnClickListener(view -> {
            Intent intent = new Intent(WxPayBlance.this, WxPayAdd.class);
            startActivity(intent);
           /* if (isAuth == 1){
                Intent intent = new Intent(WxPayBlance.this, WxPayAdd.class);
                startActivity(intent);
            }else {
                TipDialog tipDialog = new TipDialog(mContext);

                tipDialog.setmConfirmOnClickListener("您还未实名认证，请前往实名认证", new TipDialog.ConfirmOnClickListener() {
                    @Override
                    public void confirm() {
                        tipDialog.dismiss();
                    }
                });
                tipDialog.show();
            }*/

        });

        mWithdrawTv.setOnClickListener(view -> {
            if (isAuth == 1){
//                Intent intent = new Intent(WxPayBlance.this, QuXianActivity.class);
                Intent intent = new Intent(WxPayBlance.this, BankWithdrawActivity.class);
                startActivity(intent);
            }else {
                /*TipDialog tipDialog = new TipDialog(mContext);

                tipDialog.setmConfirmOnClickListener("您还未实名认证，请前往实名认证", new TipDialog.ConfirmOnClickListener() {
                    @Override
                    public void confirm() {
                        tipDialog.dismiss();
                    }
                });
                tipDialog.show();*/
                //直接跳转到认证页面
                Intent intent = new Intent(WxPayBlance.this, AuthUserInfoActivity.class);
                startActivity(intent);
            }
        });

        /**
         * 扫码充值、审核提现
         */
        mScanRechargeTv.setOnClickListener(view -> {
            Intent intent = new Intent(WxPayBlance.this, ScanRechargeActivity.class);
            startActivity(intent);
        });

        mScanWithdrawTv.setOnClickListener(view -> {
            Intent intent = new Intent(WxPayBlance.this, ScanWithdrawActivity.class);
            startActivity(intent);
        });

        findViewById(R.id.tvPayPassword).setOnClickListener(v -> {
            Intent intent = new Intent(WxPayBlance.this, ChangePayPasswordActivity.class);
            startActivity(intent);
        });

        mBankCardTv.setOnClickListener(view ->{
            Intent intent = new Intent(WxPayBlance.this, BankCardListActivity.class);
            startActivity(intent);
        });


        String auth = UserSp.getInstance(mContext).getAuth();
        if (auth.equals("0")){//未认证
            isAuth = 0;
        }else if (auth.equals("1")){//已认证
            isAuth = 1;
        }else if (auth.equals("2")){//审核中
            isAuth = 2;
        }else if (auth.equals("3")){//未通过
            isAuth = 3;
        }else {
            isAuth = -1;
        }
    }

    private void initData() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);

        HttpUtils.get().url(coreManager.getConfig().RECHARGE_GET)
                .params(params)
                .build()
                .execute(new BaseCallback<Balance>(Balance.class) {

                    @Override
                    public void onResponse(ObjectResult<Balance> result) {
                        if (result.getResultCode() == 1 && result.getData() != null) {
                            DecimalFormat decimalFormat = new DecimalFormat("0.00");
                            Balance balance = result.getData();
                            coreManager.getSelf().setBalance(balance.getBalance());
                            mBalanceTv.setText("￥" + decimalFormat.format(((balance.getBalance()))));
                        } else {
                            ToastUtil.showErrorData(WxPayBlance.this);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        ToastUtil.showNetError(WxPayBlance.this);
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void helloEventBus(EventNotifyByTag message) {
        if (TextUtils.equals(message.tag, EventNotifyByTag.Withdraw)) {
            initData();
        }
    }

    int isAuth ;
    private void getAuthUser() {

        com.xuan.xuanhttplibrary.okhttp.HttpUtils.get()
                .url(AppConfig.AUTH_GET_REAL_USER)
                .params("userId", getUser().getUserId())
                .build()
                .execute(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        JSONObject jsonObject = JSON.parseObject(result);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
//                                {"currentTime":1612765201786,"data":"2","resultCode":1}
                                if (jsonObject.getInteger("resultCode") == 1) {
                                    UserSp.getInstance(mContext).setAuth(jsonObject.getString("data"));
                                    if (jsonObject.getString("data").equals("0")){//未认证
                                        isAuth = 0;
                                    }else if (jsonObject.getString("data").equals("1")){//已认证
                                        isAuth = 1;
                                    }else if (jsonObject.getString("data").equals("2")){//审核中
                                        isAuth = 2;
                                    }else if (jsonObject.getString("data").equals("3")){//未通过
                                        isAuth = 3;
                                    }else {
                                        isAuth = -1;
                                    }
                                }
                            }
                        });

                    }
                });
    }
}
